package ntd.util.frame.action.iot.bo;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.*;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.modular.iot.bean.ThingsAttributeDo;
import lombok.Data;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.action.iot.cache.SmartEmissionAgreementCache;

import java.util.List;
import java.util.Map;

/**
 * 智能排污协议对象
 */
@Data
public class SmartEmissionAgreementBo {

    /** 一号阀门 */
    private boolean valve;

    /** 二号阀门 */
    private boolean valve2;

    /** 断电状态 */
    private boolean powerOff;

    /** 自动运行 */
    private boolean autoRun;

    /**
     * 状态字符串 2 进制 - 光耦状态
     *
     * 继电器： 1：关，0：开
     * 1：排污
     * 2：应急
     * 3：水泵
     *
     * 光耦 - 0 有信号
     * 12345678
     * 0：排污开
     * 1：排污关
     * 2：断电
     * 3：应急开
     * 4：应急关
     * 5：
     *
     * @param hexStr
     * @return
     */
    public static SmartEmissionAgreementBo init(String hexStr) {
        SmartEmissionAgreementBo smartEmissionAgreementBo = new SmartEmissionAgreementBo();

        // 转化2进制
        String binaryStr = StrUtil.padPre(NumberUtil.getBinaryStr(HexUtil.hexToInt(hexStr)), 8, "1");
        // 逆序
        String[] binaryArr = ArrayUtil.reverse(StrUtil.split(binaryStr, 1));

        // 一号阀状态
        smartEmissionAgreementBo.setValve(binaryArr[0].equals("0"));
        // 二号阀状态
        smartEmissionAgreementBo.setValve2(binaryArr[3].equals("0"));
        // 断电状态
        smartEmissionAgreementBo.setPowerOff(binaryArr[2].equals("0"));

        return smartEmissionAgreementBo;
    }

    public static SmartEmissionAgreementBo init(List<ThingsAttributeDo> thingsAttributeDoList) {
        SmartEmissionAgreementBo smartEmissionAgreementBo = new SmartEmissionAgreementBo();
        Map<String, ThingsAttributeDo> thingsAttributeDoMap = CollStreamUtil.toIdentityMap(thingsAttributeDoList, ThingsAttributeDo::getCode);
        if (ObjectUtil.isNull(thingsAttributeDoMap.get("blowdown_valve"))) {
            throw new ComException("找不到配置排污阀信息,请联系管理员...");
        }
        if (ObjectUtil.isNull(thingsAttributeDoMap.get("emergency_valve"))) {
            throw new ComException("找不到配置应急阀信息,请联系管理员...");
        }
        smartEmissionAgreementBo.setValve(BooleanUtil.toBoolean(thingsAttributeDoMap.get("blowdown_valve").getVal()));
        smartEmissionAgreementBo.setValve2(BooleanUtil.toBoolean(thingsAttributeDoMap.get("emergency_valve").getVal()));
        if (ObjectUtil.isNotNull(thingsAttributeDoMap.get("auto_manager"))) {
            smartEmissionAgreementBo.setAutoRun(BooleanUtil.toBoolean(thingsAttributeDoMap.get("auto_manager").getVal()));
        } else {
            smartEmissionAgreementBo.setAutoRun(false);
        }
        return smartEmissionAgreementBo;
    }

    /**
     * 转化为控制指令 - 2进制
     */
    public String toBinaryCmdStr() {
        return StrUtil.format("{}{}", bool2Str(this.valve2), bool2Str(this.valve));
    }

    /**
     * 转化为控制指令 - 16进制
     * @return
     */
    public String toHexCmdStr() {
        return HexUtil.toHex(NumberUtil.binaryToInt(toBinaryCmdStr())).toUpperCase();
    }

    private String bool2Str(boolean bool) {
        return bool ? "0" : "1";
    }

    public static void main(String[] args) {
        SmartEmissionAgreementBo init = init("EE");
        StaticLog.info(JSONUtil.toJsonStr(init));

        // ##0362ST=32;CN=2051;QN=20230313145147438;PW=123456;MN=41010501G00001;Flag=1;CP=&&DataTime=20230313145000;B01-Min=0.00,B01-Avg=0.00,B01-Max=0.00,B01-Cou=0.00;011-Min=0.000,011-Avg=0.000,011-Max=0.000,011-Cou=0.000;101-Min=0.000,101-Avg=0.000,101-Max=0.000,101-Cou=0.000;001-Min=0.000,001-Avg=0.000,001-Max=0.000;060-Min=0.000,060-Avg=0.000,060-Max=0.000,060-Cou=0.000&&D4C1
        // 

    }
}
